<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
model('core/core')->checklogin();
if($_RQ['op']=='main'){
		$id = intval($_RQ['id']);
		$return['pluginset']=$basic=$this->plugin_setting('setting');
		$_SESSION['sns']['id']=$id;
		if (empty($id)) 
		{
			exi('参数错误','error','/index/');
		}
		$set = $this->plugin_setting('setting');
		$board = pdo_get('jy_sns_board',array('id'=>$id));
		$board['banner']=$board['banner']?tomedia($board['banner']):$set['banner'];
		if (empty($board)) 
		{	
			exi('未找到版块','error','/index/');	
		}
		$set = $this->plugin_setting('setting');
		if ($set['imagesnum'] == 0) 
		{
			$set['imagesnum'] = 5;
		}
		$return['set']=$set;
		$return['board']=$board;
		$member = getMember($_SESSION['uid']);
		$isManager = isManager($board['id']);
		$isSuperManager = isSuperManager();
		$postcount = getPostCount($board['id']);
		$followcount = getFollowCount($board['id']);
		$isfollow = isFollow($board['id']);
		$isafollow =pdo_getcolumn('core_fans',array('mid'=>$_SESSION['uid']),'follow');
		$followtip = ((empty($set['followtip']) ? '想要和社友互动吗？需要您关注我们的公众号，点击【确定】关注后再来吧~' : $set['followtip']));		
		$tops = getTops($board['id']);
		$catelist = pdo_getall('jy_sns_complaincate',array('uniacid'=>$_SESSION['uniacid']));
		$canpost = true;
		if (!($isManager) && !($isSuperManager)) 
		{
			$check = check($member, $board, true);
			$canpost = !(is_error($check));
		}
		if (!empty($bid)) {
				$condition=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'pid'=>0,'isboardtop'=>1);
		}else{
				$condition=array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'pid'=>0,'istop'=>1);
		}
		$pindex = max(1, intval($_RQ['page']));
		$psize = 20;
		$params=array('uniacid'=>$_SESSION['uniacid']);
		$orderby='id desc';
		$total='';
		$return['tielist']=pdo_getslice('jy_sns_post',array('uniacid'=>$_SESSION['uniacid'],'deleted'=>0,'pid'=>0,'istop'=>0,'isboardtop'=>0),array($pindex,$psize),$total,$fields,'',$orderby);
		$return['postcount']=$postcount;
		$return['followcount']=$followcount;
		$return['isfollow']=$isfollow;
		$return['isafollow']=$isafollow;
		$return['canpost']=$canpost;
		$return['member']=$member;
		$return['check']=$check;
		$return['catelist']=$catelist;
		$return['tops']=$tops;
		$return['isManager']=$member;
		$return['isSuperManager']=$check;
		$return['params']=array('order'=>'new');
		$return['sharedata']=array(
			'title'=>$board['title']?:'全网论坛',
			'desc'=>$basic['share_desc'],
			'link'=>custom_url('jy_sns/board.index//id='.$id,true),
			'imgUrl'=>ATTACHMENT_URL.$basic['share_icon'],
		);
		set_html('title',$board['title']);
		exi($return);
}
if($_RQ['op']=='add'){
	$id = intval($_RQ['id']);
	$return['boards']=pdo_getall('jy_sns_board',array('uniacid'=>$_SESSION['uniacid'],'enabled'=>1),array('id','title'));
	if(!$id && $return['boards'][0]['id']){
		$id=$return['boards'][0]['id'];
	}
	$return['item']=array('id'=>$id);
	exi($return);
}
if($_RQ['op']=='follow'){
	$post=get_postdata();
	$id =$post['id'];
	if(empty($id)){
		exi('请选择论坛板块！','error','/index/');
	}
	pdo_insert('jy_sns_board_follow',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime'=>time(),'bid'=>$id));
	exi('关注成功','error','/board.index//id='.$id);
}
if($_RQ['op']=='good'){
	$post=get_postdata();
	$id =$post['id'];
	if (empty($id)) {
		exi('参数错误','error','/index/');
	}
	$l=pdo_get('jy_sns_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	if($l){
			pdo_delete('jy_sns_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			exi('jian');
	}
	pdo_insert('jy_sns_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
	exi('jia');
}
if($_RQ['op']=='post'){
		$post=get_postdata();
		if(!$post['content']){
			exi('话题内容至少十个字符','error');
		}
		if(!$post['title']){
			exi('标题至少三个字符','error');
		}
		$id = intval($post['id']);
		$board = pdo_get('jy_sns_board',array('id'=>$id));
		if (empty($id)){
			exi('请选择论坛板块！','error','/index/');
		}
		$board = pdo_get('jy_sns_board',array('id'=>$id));
		$checked = 0;		
		if ($ismanager) {
			$checked = $board['needcheckmanager'] ? 0 : 1;
		}else {
			$checked = $board['needcheck'] ? 0 : 1;
		}
		if ($issupermanager) {
			$checked = 1;
		}
		$info=pdo_get('core_members',array('id'=>$_SESSION['uid']),array('avatar','nickname'));
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'bid'=>$id ,
			'replytime'=>time(),
			'createtime'=>time(),
			'title'=>$post['title'],
			'content'=>$post['content'],
			'avatar'=>$info['avatar'],
			'images'=>ijson_encode($post['images']),
			'nickname'=>$info['nickname'],
			'checked'=>$checked
		);
		pdo_insert('jy_sns_post',$data);
		if($board['postcredit']){
			pdo_update('core_members',array('credit1 +='=>$board['postcredit']),array('id'=>$_SESSION['uid']));
		}
		exi('发表成功','','/index/');
}
?>